'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public MustInherit Class Snac
    Implements ISerializable

    Public Const SizeFixPart As Integer = 10

    Private _DataSize As Integer

    Public Sub New(ByVal serviceId As Integer, ByVal subtypeId As Integer)
        _ServiceId = serviceId
        _SubtypeId = subtypeId
    End Sub

    Public Shared Function GetKey(ByVal snac As Snac) As String
        Return String.Format("{0:X2},{1:X2}", If(snac.SubtypeId = 1, 1, snac.ServiceId), snac.SubtypeId)
    End Function

    Private _ServiceId As Integer
    Public Property ServiceId() As Integer
        Get
            Return _ServiceId
        End Get
        Set(ByVal value As Integer)
            _ServiceId = value
        End Set
    End Property

    Private _SubtypeId As Integer
    Public Property SubtypeId() As Integer
        Get
            Return _SubtypeId
        End Get
        Set(ByVal value As Integer)
            _SubtypeId = value
        End Set
    End Property

    Private _Flags As Integer
    Public Property Flags() As Integer
        Get
            Return _Flags
        End Get
        Set(ByVal value As Integer)
            _Flags = value
        End Set
    End Property

    Private _RequestId As Long
    Public Property RequestId() As Long
        Get
            Return _RequestId
        End Get
        Set(ByVal value As Long)
            _RequestId = value
        End Set
    End Property

    Private _HasData As Boolean
    Public ReadOnly Property HasData() As Boolean Implements ISerializable.HasData
        Get
            Return _HasData
        End Get
    End Property

    Public MustOverride Function CalculateDataSize() As Integer Implements ISerializable.CalculateDataSize

    Public Function CalculateTotalSize() As Integer Implements ISerializable.CalculateTotalSize
        Return SizeFixPart + CalculateDataSize()
    End Function

    Public ReadOnly Property SnacDataSize() As Integer Implements ISerializable.DataSize
        Get
            Return _DataSize
        End Get
    End Property

    Public ReadOnly Property SnacTotalSize() As Integer Implements ISerializable.TotalSize
        Get
            Return SizeFixPart + SnacDataSize
        End Get
    End Property

    Protected Sub SetTotalSize(ByVal value As Integer)
        _DataSize = value - SizeFixPart
    End Sub

    Public Overridable Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte)) Implements ISerializable.Deserialize
        _ServiceId = ByteConverter.ToUInt16(data.GetRange(0, 2))
        _SubtypeId = ByteConverter.ToUInt16(data.GetRange(2, 2))
        _Flags = ByteConverter.ToUInt16(data.GetRange(4, 2))
        _RequestId = ByteConverter.ToUInt16(data.GetRange(6, 4))

        _HasData = True
    End Sub

    Private Shared snacRequestId As Long = 0

    Public Overridable Function Serialize() As System.Collections.Generic.List(Of Byte) Implements ISerializable.Serialize
        Dim data As List(Of Byte)

        _DataSize = CalculateDataSize()

        data = New List(Of Byte)(SizeFixPart + _DataSize)

        _RequestId = Threading.Interlocked.Increment(snacRequestId)

        data.AddRange(ByteConverter.GetBytes(Convert.ToUInt16(_ServiceId)))
        data.AddRange(ByteConverter.GetBytes(Convert.ToUInt16(_SubtypeId)))
        data.AddRange(ByteConverter.GetBytes(Convert.ToUInt16(_Flags)))
        data.AddRange(ByteConverter.GetBytes(Convert.ToUInt32(_RequestId)))

        Return data
    End Function
End Class


Public Class SnacDescriptor
    Private _ServiceId As Integer
    Public Property ServiceId() As Integer
        Get
            Return _ServiceId
        End Get
        Set(ByVal value As Integer)
            _ServiceId = value
        End Set
    End Property

    Private _SubtypeId As Integer
    Public Property SubtypeId() As Integer
        Get
            Return _SubtypeId
        End Get
        Set(ByVal value As Integer)
            _SubtypeId = value
        End Set
    End Property

    Private _Flags As Integer
    Public Property Flags() As Integer
        Get
            Return _Flags
        End Get
        Set(ByVal value As Integer)
            _Flags = value
        End Set
    End Property

    Private _RequestId As Long
    Public Property RequestId() As Long
        Get
            Return _RequestId
        End Get
        Set(ByVal value As Long)
            _RequestId = value
        End Set
    End Property

    Public Shared Function GetKey(ByVal descriptor As SnacDescriptor) As String
        Return String.Format("{0:X2},{1:X2}", If(descriptor.SubtypeId = 1, 1, descriptor.ServiceId), descriptor.SubtypeId)
    End Function

    Public Overridable Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        _ServiceId = ByteConverter.ToUInt16(data.GetRange(0, 2))
        _SubtypeId = ByteConverter.ToUInt16(data.GetRange(2, 2))
        _Flags = ByteConverter.ToUInt16(data.GetRange(4, 2))
        _RequestId = ByteConverter.ToUInt16(data.GetRange(6, 4))
    End Sub

    Public Shared Function GetDescriptor(ByVal offset As Integer, ByVal bytes As System.Collections.Generic.List(Of Byte)) As SnacDescriptor
        Dim data As List(Of Byte) = bytes.GetRange(offset, bytes.Count - offset)
        Dim desc As SnacDescriptor = New SnacDescriptor
        desc.Deserialize(data)
        Return desc
    End Function
End Class
